"""
src/odelib/simstate.py
    A singleton class which keeps tabs on the state of the sim so we don't have
    to pass timesteps, current sim time, etc. all around the code. Anybody who
    needs that information in their 'Update' functions can just create an instance
    of this class and get them.

Written By:
    James Thomas
    Email: jim@houseoftechnology.org
    Web: http://mission-cognition.houseoftechnology.org/

Copyright 2009-2012

This file is part of the PandaODElib distribution.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 3 of
the License, or (at your option) any later version. The text of the
GNU Lesser General Public License is included with this library in
the file named LICENSE.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
"""

class SimState(object):
    """A singleton class which keeps tabs on the state of the sim so we don't have
    to pass timesteps, current sim time, etc. all around the code. Anybody who
    needs that information in their 'Update' functions can just create an instance
    of this class and get them."""
    __singleInstance = None # This will hold the single instance

    def __new__(cls, *args, **kwargs):
        # Need to check to see if __singleInstance is assigned in this class
        # The trick is to compare class types instead of just testing for none.
        # This way you can subclass Primitives and it will still work.
        if cls != type(cls.__singleInstance):
            # These don't match so therefore cls.__singleInstance is None
            # and we need to create the object.
            cls.__singleInstance = object.__new__(cls)
        return cls.__singleInstance

    def __init__(self):
        try:
            # If we have already done our initialization the
            # value will be set and an exception won't be raised
            return self.__initRetValue
        except AttributeError:
            # First time running init, set our return value
            # so we do the initialization again
            self.__initRetValue = None

        self.simTime = 0
        self.timeStep = 0
        self.simLoop = 0
    
